package com.smedi.ismedi.authoritymanagement.core.application.service.impl;

import com.smedi.ismedi.authoritymanagement.core.application.command.RolePermissionDetailCommand;
import com.smedi.ismedi.authoritymanagement.core.application.service.RolePermissionDetailApplicationService;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.FrameStructure;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.RoleBasic;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.RolePermissionDetail;
import com.smedi.ismedi.authoritymanagement.core.domain.repository.FrameStructureRepository;
import com.smedi.ismedi.authoritymanagement.core.domain.repository.RoleBasicRepository;
import com.smedi.ismedi.authoritymanagement.core.domain.service.RolePermissionDetailDomainService;
import com.smedi.ismedi.authoritymanagement.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-8-17 15:20
 */
@Slf4j
@Service
@AllArgsConstructor
public class RolePermissionDetailApplicationServiceImpl implements RolePermissionDetailApplicationService {
    private final DozerUtil dozerUtil;

    private final FrameStructureRepository frameStructureRepository;

    private final RoleBasicRepository roleBasicRepository;

    private final RolePermissionDetailDomainService rolePermissionDetailDomainService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveRolePermissionDetail(String rootCode, Long roleId, List<RolePermissionDetailCommand> rolePermissionDetailCommands) {
        FrameStructure root = frameStructureRepository.selectByRootCode(rootCode);
        if (root == null) {
            throw LogicException.raise(CustomError.FRAME_STRUCTURE_NOT_EXIST);
        }

        RoleBasic role = roleBasicRepository.selectByRoleId(roleId);
        if (role == null) {
            throw LogicException.raise(CustomError.ROLE_BASIC_NOT_EXIST);
        }

        List<RolePermissionDetail> rolePermissionDetails = dozerUtil.convertor(rolePermissionDetailCommands, RolePermissionDetail.class);
        rolePermissionDetails.stream().forEach(rolePermissionDetail -> {
            if (StringUtils.isEmpty(rolePermissionDetail.getStructureId())) {
                throw LogicException.raise(CustomError.FRAME_STRUCTURE_ID_MISS);
            }

            if (frameStructureRepository.selectByStructureId(rolePermissionDetail.getStructureId()) == null) {
                throw LogicException.raise(CustomError.FRAME_STRUCTURE_ID_NOT_EXIST);
            }

            rolePermissionDetail.setRoleId(roleId)
                    .setRootCode(rootCode)
                    .setRolePermissionDetailId(null);
        });

        rolePermissionDetailDomainService.batchSaveRolePermissionDetail(rootCode, roleId, rolePermissionDetails, true);
    }
}
